<?php

namespace app\common\model\xiluhouse;

use think\Model;
use traits\model\SoftDelete;

class Article extends Base
{

    use SoftDelete;



    // 表名
    protected $name = 'xiluhouse_article';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'createtime_text',
        'author_avatar_text',
        'image_text',
        'classify_text',
        'status_text'
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return time_tran($value);
    }
    public function getAuthorAvatarTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['author_avatar']) ? $data['author_avatar'] : '');
        return $value?getAvatar($value, $data['author']):'';
    }


    public function getClassifyList()
    {
        return ['1' => __('Classify 1'), '2' => __('Classify 2'), '3' => __('Classify 3'), '4' => __('Classify 4')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'hidden' => __('Status hidden')];
    }


    public function getClassifyTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['classify']) ? $data['classify'] : '');
        $list = $this->getClassifyList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function articlecategory()
    {
        return $this->belongsTo('ArticleCategory', 'article_category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
